home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- ditgray - dither a grayscale image down to a few colors.
- usage: ditgray <infile.bw> <outfile.ci>
-
- Tim Heidmann, Silicon Graphics
- December 7, 1992
- January 11, 1993 Add LEVEL5 and LEVEL8 ditherers (for rgb->ci tools)
- March 31, 1993 getrow() workaround
- */
- #include <gl/image.h>
- #include <stdio.h>
- #include <math.h>
-
- #ifdef SHADOW
- int map[] = {0,255};
- int omap[] = {1,0};
- #else
- #ifdef LEVEL5
- int map[] = { 0, 63, 127, 191, 255};
- int omap[] = { 0, 1, 2, 3, 4};
- #else
- #ifdef LEVEL8
- int map[] = { 0, 37, 73, 110, 146, 183, 219, 255};
- int omap[] = { 0, 1, 2, 3, 4, 5, 6, 7};
- #else
- int map[] = { -1, 0, 128, 255};
- int omap[] = { 0, 1, 2, 3};
- #endif
- #endif
- #endif
- int nMap = sizeof(map) / sizeof(int);
-
- main(int argc, char *argv[]) {
- short ibuf[4096], obuf[4096];
- IMAGE *inf, *outf;
- int irow, icol, iRand, iMap;
-
- if (argc != 3) {
- fprintf(stderr, "usage: %s <infile.bw> <outfile.ci>\n", argv[0]);
- exit(1);
- }
- if ((inf = iopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "Cannot open image file %s\n", argv[1]);
- exit(1);
- }
- if (inf->zsize != 1)
- fprintf(stderr, "Warning: %s has more than one channel.\n", argv[1]);
-
- /* workaround 3/31/93 - Fix getrow() only returns 2bpp images correctly
- outf = iopen(argv[2], "w", RLE(1), 3, inf->xsize, inf->ysize, 1);
- */
- outf = iopen(argv[2], "w", RLE(2), 3, inf->xsize, inf->ysize, 1);
- if (outf == NULL) {
- fprintf(stderr, "Cannot open image file %s\n", argv[1]);
- exit(1);
- }
-
- outf->colormap = CM_SCREEN;
- for (irow = 0; irow < inf->ysize; irow++) {
- getrow(inf, ibuf, irow, 0);
- for (icol = 0; icol < inf->xsize; icol++) {
- /* Find iMap s.t. map[iMap - 1] < ibuf[icol] <= map[iMap] */
- for (iMap = 0; iMap < nMap && map[iMap] < ibuf[icol]; iMap++) ;
- if (iMap == 0) obuf[icol] = omap[0];
- else if (iMap == nMap) obuf[icol] = omap[nMap - 1];
- else {
- iRand = random() % (map[iMap] - map[iMap-1]) + map[iMap-1] + 1;
- obuf[icol] = omap[(ibuf[icol] >= iRand) ? iMap : (iMap - 1)];
- }
- }
- putrow(outf, obuf, irow, 0);
- }
-
- iclose(inf);
- iclose(outf);
- }
-